package ui;

import java.io.ByteArrayOutputStream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;
import javax.microedition.media.control.VideoControl;

import midlet.VadeMecumMIDlet;
import multimedia.SnapshotThread;
import multimedia.StartRecordThread;
import multimedia.StopRecordThread;

public class VadeMecumCameraCanvas extends Canvas {
	
	//the base midlet
	public VadeMecumMIDlet midlet = null;
	
	public Player capturePlayer = null;
	
	//the video control
	public VideoControl videoControl = null;
	
	//the record control
	private RecordControl recordControl = null;
	
	//the record stream
	ByteArrayOutputStream recordStream = null;
	
	//record or snapshot?
	private boolean record = false;
	
	//is recording?
	private boolean isRecording = false;
	
	//commands
	public Command snapshot = null;
	public Command initRecording = null;
	public Command stopRecording = null;
	public Command exitCommand = null;
	
	public VadeMecumCameraCanvas(VadeMecumMIDlet midlet, boolean record) throws Exception {
		this.midlet = midlet;
		
		this.record = record;
		
		exitCommand = new Command("Back", Command.BACK, 1);
		addCommand(exitCommand);
		
		if (this.record) {
			if(System.getProperty("supports.video.capture").equals("true")) {
				capturePlayer = Manager.createPlayer("capture://video?encoding=video/mp4");
				capturePlayer.realize();
			} else {
				midlet.error(new Exception("Device not supports video capture!!"));
			}
			recordControl = (RecordControl) capturePlayer.getControl("javax.microedition.media.control.RecordControl");
			if (recordControl == null) {
				throw new Exception("No record control found!");
			}
			initRecording = new Command("Init record", Command.OK, 1);
			stopRecording = new Command("Stop record", Command.CANCEL, 1);
			addCommand(initRecording);
		} else {
			capturePlayer = Manager.createPlayer("capture://video");
			capturePlayer.realize();
			snapshot = new Command("Take a picture", Command.OK, 1);
			addCommand(snapshot);
		}
		
		//initializes the video control
		videoControl = (VideoControl) capturePlayer.getControl("javax.microedition.media.control.VideoControl");		
		if (videoControl == null) {
			throw new Exception("No video control for capturing!");
		}	
		
		//init display mode and to use direct video and this canvas
		videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
		
		try {
			videoControl.setDisplayFullScreen(true);
		} catch (MediaException me) {
			//some devices may not support
			videoControl.setDisplayLocation(5, 5);
			try {
				videoControl.setDisplaySize(getWidth() - 10, getHeight() - 10);
			} catch (Exception e) {
				//NOP
			}
			repaint();
		}
		
		try {
			this.capturePlayer.start();
		} catch (MediaException e) {
			e.printStackTrace();
		}
		videoControl.setVisible(true);
	}
	
	public VideoControl getVideoControl() {
		return videoControl;
	}

	public void setVideoControl(VideoControl videoControl) {
		this.videoControl = videoControl;
	}
	
	public void paint(Graphics g) {
		//clear background
		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());
		
		//and draw a rectangle with a different color
//		g.setColor(0x44ff66);
//		g.drawRect(2, 2, getWidth() - 4, getHeight() - 4);
	}
	
	public void takeSnapshot() {
		Thread snapThread = new Thread(new SnapshotThread(this));
		snapThread.start();
	}

	public void startRecording() {
		Thread startThread = new Thread(new StartRecordThread(this));
		startThread.start();
	}
	
	public void stopRecording() {
		Thread stopThread = new Thread(new StopRecordThread(this));
		stopThread.start();
	}

	public void setRecording(boolean b) {
		this.isRecording = b;		
	}
	
	public boolean isRecording() {
		return isRecording;
	}

	public RecordControl getRecordControl() {
		return recordControl;
	}

	public void setRecordControl(RecordControl recordControl) {
		this.recordControl = recordControl;
	}

}
